package com.itheima.mm.service.impl;

import com.itheima.mm.dao.*;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.pojo.Course;
import com.itheima.mm.pojo.Question;
import com.itheima.mm.pojo.Tag;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.QuestionService;


import com.itheima.mm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private CatalogDao catalogDao;
    @Autowired
    private DictDao dictDao;
    @Autowired
    private IndustryDao industryDao;
    @Autowired
    private TagDao tagDao;
    @Autowired
    private ReviewLogDao reviewLogDao;


    @Override
    public List<Question> findListByPage(QueryPageBean queryPageBean){
        List<Question> questions = new ArrayList<>();
        /*从数据库问题表中获取问题类型的数据*/
        questions=questionDao.findListByPage(queryPageBean);
        /*补全question对象*/
        for (Question question : questions) {
            question = replenishQuestion(question);
        }
        return questions;
    }


    @Override
    public Long findTotal(QueryPageBean queryPageBean) {
        return questionDao.findTotal(queryPageBean);

    }

    @Override
    public List<Question> findClassicListByPage(QueryPageBean queryPageBean) {

        List<Question> questions =questionDao.findListByPage(queryPageBean);
        /*从数据库问题表中获取问题类型的数据*/

        /*补全question对象*/
        for (Question question : questions) {
            question = replenishQuestion(question);
        }
        return questions;

    }

    @Override
    public Question findById(Integer id) {
        Question question=questionDao.findById(id);
        question=replenishQuestion(question);
        return question;
    }

    @Override
    public void add(Question question) {
        if (question.getId()==0) {
            questionDao.add(question);
            reviewLogDao.addNewQuestionLog(question, DateUtils.parseDate2String(new Date()));
        }else {
            questionDao.update(question);
            questionDao.delectQuestionItemByQuestionID(question);
            questionDao.delectTagQuesByQuestionID(question);
        }
        questionDao.addQuestionItem(question);
        questionDao.addTagQues(question);
    }

    @Override
    public boolean updateClassic(Integer questionId) {
        Question byId = questionDao.findById(questionId);
        Integer isClassic = byId.getIsClassic();
        if (isClassic == 0) {
            questionDao.updateClassic(questionId);
        }else {
            return false;
        }
        return true;
    }

    /*将问题中的其他属性补齐*/
    private Question replenishQuestion(Question question){


        /*根据courseId查course的名字*/
        question.setCourseName(courseDao.findCourseById(question.getCourseId()).getName());
        /*根据userId去查询user的名字*/
        question.setCreator(userDao.findById(question.getUserId()).getUsername());
        /*根据自己的id去查询用户使用的次数*/
        question.setUsedQty(String.valueOf(questionDao.findUsedQtyById(question.getId())));
        /*获取企业*/
        question.setCompany(companyDao.findById(question.getCompanyId()));
        /*获取企业所在地址*/
        question.getCompany().setCity(dictDao.findNameListById(question.getCompany().getCityId()));
        /*获取企业行业方向*/
        question.getCompany().setIndustryList(industryDao.findListByCompanyId(question.getCompany().getId()));
        /*获取企业的行业方向的名字的列表*/
        question.getCompany().setIndustryNameList(industryDao.findNameByCompanyId(question.getCompany().getId()));
        /*获取catalog*/
        question.setCatalog(catalogDao.findListById(question.getCatalogId()));
        /*获取选项信息*/
        question.setQuestionItemList(questionDao.findQuestionItemListByQuestionId(question.getId()));
        /*获取标签信息*/
        question.setTagList(tagDao.findListByQuestionId(question.getId()));
        /*获取tag名字的列表*/
        ArrayList<String> tagName = new ArrayList<>();
        for (Tag tag : question.getTagList()) {
            tagName.add(tag.getName());
        }

        question.setTagNameList(tagName);
        /*获取最后一条审核日志*/
        question.setReviewLog(reviewLogDao.findLastReviewLog(question.getId()));


        /*设置基本信息补全*/
        /*question.setNumber("0");
        question.setRemark("0");*/
        /*获取是否是名企(微信)*/
        question.setIsFamous(companyDao.findById(question.getCompanyId()).getIsFamous());
        /*获取标签(微信)*/
        question.setTags(tagDao.findListByQuestionId(question.getId()));
        question.setIsFinished(0);
        question.setIsFavorite(0);






        return question;
    }
}
